#include "form_login.h"
#include "ui_form_login.h"

#include <QAbstractSocket>


extern QString LOGIN;
extern QString DELIVERY;
extern QString PICKUP;
extern QString STATUS;
extern QString BIN_NUM;
extern QString ServerIp;
extern QString Port;


Form_login::Form_login(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Form_login)
{
    ui->setupUi(this);

    num_or_pwd = 0;

    socket = new QTcpSocket(this);

    connect(socket,SIGNAL(connected()),this,SLOT(connectedSlot()));
    connect(socket, SIGNAL(readyRead()), this, SLOT(readSlot()));


    ui->pwd->setEchoMode(QLineEdit::Password);
    ui->checkBox->setChecked(false);


    connect(&fdlv, SIGNAL(return_login()), this, SLOT(exchangeLoginSlot()));
}

Form_login::~Form_login()
{
    delete ui;
}

int Form_login::get_info(QString recv)
{
    if(recv.length() == 0)
        return -1;
    QStringList list;
    list = recv.split("#");
    qDebug() << "list->length()" << list.length();

    int i;
    for(i = 0; i < list.length(); i++)
    {
        qDebug() << "list->at(" << i << "):" << list.at(i);
    }
    QString tmp = list.last();
    QStringList list2;
    list2 = tmp.split("\r");
    last = list2.first();
    qDebug() << "getinfo" << last;
    return 0;
}

void Form_login::connectedSlot()
{
    qDebug() << "connected success";
    send_login_request();
    qDebug() << "send login";
}

void Form_login::readSlot()
{
    qDebug() << "read readly";
    read_login_request();
    qDebug() << "read login";
}

void Form_login::read_login_request()
{
    QByteArray arr;
    QString recv;

    arr.clear();
    arr = socket->readAll();
    recv = QString(arr);

    get_info(recv);
    qDebug() << last;

    if(last == "0")
    {
        qDebug() << "login success, next interface,send/fetch";
        //socket->disconnected();
        this->hide();
        fdlv.show();
    }
    else
    {
        QMessageBox::warning(this, "login", "count or pwd error!");
        clear_all();
        qDebug() << "fail login";
    }

}

void Form_login::send_login_request()
{

    QString PHONE_COURIER = ui->phone_courier->text();
    QString PWD = ui->pwd->text();

    QString send;
    send.sprintf("%s#%s#%s#%s#", LOGIN.toStdString().data(), BIN_NUM.toStdString().data()\
                 , PHONE_COURIER.toStdString().data(), PWD.toStdString().data());

    QByteArray arr;
    arr.clear();
    arr.append(send);

    qDebug() << "send length:" << socket->write(arr);
    qDebug() << send;

    clear_all();
}

/* sure */
void Form_login::on_btn_sure_clicked()
{
    num_or_pwd++;

    if(num_or_pwd >= 2)
    {
        if(ui->phone_courier->text().isEmpty() || ui->pwd->text().isEmpty())
        {
            num_or_pwd = 0;
            return;
        }
    #if 0
        this->hide();
        fdlv.show();
    #endif
        socket->abort();//abort connected
        socket->connectToHost(ServerIp, 8888);
        qDebug() << "tyuiop";
    }
}

void Form_login::exchangeLoginSlot()
{
    this->show();
    fdlv.hide();
}

/* del */
void Form_login::on_btn_del_clicked()
{
    if(num_or_pwd == 0)
    {
        QString str = ui->phone_courier->text();
        int len = str.length();
        QString tmp = str.mid(0, len-1);
        ui->phone_courier->setText(tmp);
    }
    if(num_or_pwd == 1)
    {
        QString str = ui->pwd->text();
        int len = str.length();
        QString tmp = str.mid(0, len-1);
        ui->pwd->setText(tmp);
    }
}

/* rewrite */
void Form_login::on_btn_rewrite_clicked()
{
    clear_all();
}
/* return */
void Form_login::on_btn_ret_clicked()
{
    clear_all();
    emit return_welcome();
}
/* quit */
void Form_login::on_btn_quit_clicked()
{
    clear_all();
    emit return_welcome();
}
/* clear all */
void Form_login::clear_all()
{
    ui->phone_courier->setText("");
    ui->pwd->setText("");
    num_or_pwd = 0;
}

void Form_login::on_checkBox_clicked(bool checked)
{
    if(true == checked)
    {
        ui->pwd->setEchoMode(QLineEdit::Normal);
    }
    else
    {
        ui->pwd->setEchoMode(QLineEdit::Password);
    }
}

void Form_login::on_btn_0_clicked()
{
    if(num_or_pwd == 0)
        ui->phone_courier->setText(ui->phone_courier->text().append("0"));
    if(num_or_pwd == 1)
        ui->pwd->setText(ui->pwd->text().append("0"));
}

void Form_login::on_btn_1_clicked()
{
    if(num_or_pwd == 0)
        ui->phone_courier->setText(ui->phone_courier->text().append("1"));
    if(num_or_pwd == 1)
        ui->pwd->setText(ui->pwd->text().append("1"));
}

void Form_login::on_btn_2_clicked()
{
    if(num_or_pwd == 0)
        ui->phone_courier->setText(ui->phone_courier->text().append("2"));
    if(num_or_pwd == 1)
        ui->pwd->setText(ui->pwd->text().append("2"));
}

void Form_login::on_btn_3_clicked()
{
    if(num_or_pwd == 0)
        ui->phone_courier->setText(ui->phone_courier->text().append("3"));
    if(num_or_pwd == 1)
        ui->pwd->setText(ui->pwd->text().append("3"));
}

void Form_login::on_btn_4_clicked()
{
    if(num_or_pwd == 0)
        ui->phone_courier->setText(ui->phone_courier->text().append("4"));
    if(num_or_pwd == 1)
        ui->pwd->setText(ui->pwd->text().append("4"));
}

void Form_login::on_btn_5_clicked()
{
    if(num_or_pwd == 0)
        ui->phone_courier->setText(ui->phone_courier->text().append("5"));
    if(num_or_pwd == 1)
        ui->pwd->setText(ui->pwd->text().append("5"));
}

void Form_login::on_btn_6_clicked()
{
    if(num_or_pwd == 0)
        ui->phone_courier->setText(ui->phone_courier->text().append("6"));
    if(num_or_pwd == 1)
        ui->pwd->setText(ui->pwd->text().append("6"));
}

void Form_login::on_btn_7_clicked()
{
    if(num_or_pwd == 0)
        ui->phone_courier->setText(ui->phone_courier->text().append("7"));
    if(num_or_pwd == 1)
        ui->pwd->setText(ui->pwd->text().append("7"));
}

void Form_login::on_btn_8_clicked()
{
    if(num_or_pwd == 0)
        ui->phone_courier->setText(ui->phone_courier->text().append("8"));
    if(num_or_pwd == 1)
        ui->pwd->setText(ui->pwd->text().append("8"));
}

void Form_login::on_btn_9_clicked()
{
    if(num_or_pwd == 0)
        ui->phone_courier->setText(ui->phone_courier->text().append("9"));
    if(num_or_pwd == 1)
        ui->pwd->setText(ui->pwd->text().append("9"));
}
